<?php

class Story_WidgetController extends Core_Controller_Action_Standard {

    public function storyFollowAction() {
        if ($this->getRequest()->isPost()) {
        	$option_id = $this->getRequest()->getParam('option_id');
        	$story_id = $this->getRequest()->getParam('story_id');
        	$viewer = Engine_Api::_()->user()->getViewer();
	        $followTable = Engine_Api::_()->getDbTable('follow', 'story');
	        $row = $followTable->getFollowStory($story_id, $viewer->getIdentity());
            $row->follow = $option_id;
            $row->save();
	    }
    }

    public function unfollowAction() {
        if( !$this->_helper->requireUser()->isValid() ) return;

        // Make form
        $option_id = $this->getRequest()->getParam('option_id', 0);
        $story_id = $this->getRequest()->getParam('story_id', 0);
        if($option_id==0){
            $this->view->form = $form = new Story_Form_Unfollow();
        } else {
            $this->view->form = $form = new Story_Form_Follow();
        }

        if( !$this->getRequest()->isPost() ) {
          $this->view->status = false;
          $this->view->error = Zend_Registry::get('Zend_Translate')->_('No action taken');
          return;
        }
        if( !$form->isValid($this->getRequest()->getPost()) ) {
          $this->view->status = false;
          $this->view->error = Zend_Registry::get('Zend_Translate')->_('Invalid data');
          return;
        }

        // Process
        $viewer = Engine_Api::_()->user()->getViewer();
        $followTable = Engine_Api::_()->getDbTable('follow', 'story');
        $row = $followTable->getFollowStory($story_id, $viewer->getIdentity());
        $row->follow = $option_id;
        $row->save();
        return $this->_forward('success', 'utility', 'core', array(
        'smoothboxClose' => true,
        'parentRefresh' => true,
        'messages' => array($message)
      ));
    }
}